package com.noble.meether.chat.ui.system;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;

import com.noble.glamour.module.local.chat.utils.MsgUtils;
import com.noble.glamour.module.logic.application.ModuleMgr;
import com.noble.library.base.BaseAppCompatActivity;
import com.noble.library.observe.MsgMgr;
import com.noble.library.observe.MsgType;
import com.noble.library.observe.PObserver;
import com.noble.library.view.custom.state.CustomStatusListView;
import com.noble.library.widget.ListView.xlistview.ExListView;
import com.noble.meether.R;
import com.noble.meether.chat.ui.system.bean.SysNotice;

import java.util.List;

/**
 * 我的公告页面
 */
public class SysMessAct extends BaseAppCompatActivity implements PObserver {
    private List<SysNotice> list;
    private SysMessAdapter mSysMessAdapter;
    private ExListView exListView;
    private CustomStatusListView customStatusListView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sys_mess_act);
        initView();
    }

    private void initView() {
        setBackView();
        setTitle(getString(R.string.sys_mess_act));
        MsgUtils.removeNoticeUnread();  // 移除公告未读s
        customStatusListView = findViewById(R.id.sys_mess_list);
        exListView = customStatusListView.getExListView();
        mSysMessAdapter = new SysMessAdapter(this, null);
        exListView.setAdapter(mSysMessAdapter);
        exListView.addFooterView(LayoutInflater.from(this).inflate(R.layout.common_footer_distance, null));
        exListView.setPullRefreshEnable(false);
        exListView.setPullLoadEnable(false);
        MsgMgr.getInstance().attach(this);

        list = ModuleMgr.getCommonMgr().getNoticeList();
        if (list == null || list.size() <= 0) {
            customStatusListView.showNoData("暂无系统公告");
            return;
        }
        refreshList();
    }

    private void refreshList() {
        mSysMessAdapter.setList(list);
        exListView.setSelection(list.size() - 1);
        customStatusListView.showExListView();
    }

    @Override
    public void onMessage(String key, Object value) {
        if (key.equals(MsgType.MT_Refresh_Notice)) {
            refreshList();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        MsgMgr.getInstance().detach(this);
    }
}